package com.example.houduan.Service.impl;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.houduan.Entity.User;
import com.example.houduan.Mapper.UserMapper;
import com.example.houduan.Service.UserService;
import com.example.houduan.config.Md5Util;
import com.example.houduan.exception.BusinessException;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        // 1. 校验输入
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new BusinessException("用户名或密码不能为空");
        }

        // 2. 查询用户
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }

        // 3. 检查账号状态
        if (user.getStatus() != 1) {
            throw new BusinessException("账号已禁用");
        }

        // 4. 验证密码
        String encryptedPassword = Md5Util.md5(password + user.getSalt());
        if (!encryptedPassword.equals(user.getPassword())) {
            throw new BusinessException("密码错误");
        }

        return user;
    }
}